/* fafamtc.p FIXED ASSETS MAINTENANCE - Component Maintenance                 */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: fafamtc.p 84228 2018-09-12 13:38:42Z atu                          $: */
/* REVISION: 9.1      LAST MODIFIED: 10/26/99   BY: *N021* Pat Pigatti        */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane   */
/* REVISION: 9.1      LAST MODIFIED: 05/17/00   BY: *M0LJ* Vihang Talwalkar   */
/* REVISION: 9.1      LAST MODIFIED: 05/25/00   BY: *M0MT* Veena Lad          */
/* REVISION: 9.1      LAST MODIFIED: 07/28/00   BY: *N0BX* Arul Victoria      */
/* REVISION: 9.1      LAST MODIFIED: 08/14/00   BY: *N0L0* Jacolyn Neder      */
/* REVISION: 9.1      LAST MODIFIED: 09/28/00   BY: *M0T5* Rajesh Lokre       */
/* REVISION: 9.1      LAST MODIFIED: 11/27/00   BY: *M0WT* Vihang Talwalkar   */
/* Revision: 1.15.1.16   BY: Ashish M.         DATE: 10/11/01  ECO: *M1M1*    */
/* Revision: 1.15.1.17   BY: Alok Thacker      DATE: 03/13/02  ECO: *M1NB*    */
/* Revision: 1.15.1.18    BY: Manish Dani        DATE: 12/20/02  ECO: *M1YW*  */
/* Revision: 1.15.1.20    BY: Paul Donnelly (SB) DATE: 06/26/03  ECO: *Q00C*  */
/* Revision: 1.15.1.22    BY: Robert Jensen      DATE: 09/02/04  ECO: *N2XK*  */
/* Revision: 1.15.1.23    BY: Vandna Rohira      DATE: 12/17/04  ECO: *P2Z5*  */
/* Revision: 1.15.1.24    BY: Hitendra PV        DATE: 11/18/05  ECO: *P47Z*  */
/* Revision: 1.15.1.25    BY: Jean Miller        DATE: 03/04/09  ECO: *R1DF*  */
/* REVISION: 23537     LAST MODIFIED: 10/19/12    BY: ddo                     */
/* $Revision: 1.15.1.26 $ BY: Mallika S. Poojary   DATE: 03/04/09  ECO: *R1DC* */
/*-Revision end---------------------------------------------------------------*/
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{us/bbi/mfdeclre.i}

/* EXTERNAL LABEL INCLUDE */
{us/bbi/gplabel.i}
{us/gp/gprunpdf.i "mcpl" "p"}

/* --------------------  DEFINE QUERY  ------------------- */
define query q_fad_det
   for fad_det
   scrolling.

/* -----------------  STANDARD VARIABLES  ---------------- */
define variable p-status       as character no-undo.
define variable perform-status as character format "x(25)"
   initial "first" no-undo.
define variable p-skip-update  like mfc_logical no-undo.
define variable l-rowid        as rowid     no-undo.
define variable l-delete-it    like mfc_logical no-undo.
define variable l-del-rowid    as rowid     no-undo.
define variable l-top-rowid    as rowid     no-undo.
define variable lines          as integer
   initial 10 no-undo.
define variable l-found        like mfc_logical no-undo.
define variable pos            as integer   no-undo.
define variable l-first        like mfc_logical no-undo.
define variable l-error        like mfc_logical no-undo.
define variable l-title        as character no-undo.

/* ------------------  BUTTON VARIABLES  ----------------- */
define button b-update label "Update".
define button b-add    label "Add".
define button b-delete label "Delete".
define button b-split  label "Split".
define button b-end    label "End".

/* -------------  STANDARD WIDGET VARIABLES  ------------- */
define        variable l-focus           as widget-handle no-undo.
define        variable w-frame           as widget-handle no-undo.

/* ------------------  SCREEN VARIABLES  ----------------- */
define shared variable l-key             as character     format "x(12)" no-undo.
define shared variable l-reopen-fa-query like mfc_logical no-undo.

define        variable l-amt             as decimal       format "->>>,>>>,>>9.99<" no-undo.
define        variable l-new             as character     format "x(12)" no-undo.
define        variable l-pct             as decimal       format ">>9.99<" initial 0 no-undo.
define        variable l-err-nbr         as integer       format ">>>9" initial 0 no-undo.
define        variable l-nrm-err         like mfc_logical initial no no-undo.
define        variable fad-total         as decimal       format ">>>,>>>,>>>,>>9.999" no-undo.
define        variable fad_puramt_fmt    as character     format "x(30)" no-undo.
define        variable correct-yn        like mfc_logical initial yes no-undo.
define        variable l-total           as decimal       format ">>>,>>>,>>9.99<"
   initial 0 no-undo.
define        variable fad-cost          as decimal       format ">>>,>>>,>>9.99<"
   initial 0 no-undo.
define        variable l-create-dep      like mfc_logical no-undo.

define        variable l_amt_fmt         as character     no-undo.
define        variable mc-error-number   like msg_nbr no-undo.
define        variable rndmthd           like rnd_rnd_mthd no-undo.
define        variable l-curr            like cu_mstr.cu_curr no-undo.
define buffer bfabk_mstr for fabk_mstr.
define        variable l-book-curr       like cu_curr         no-undo.


/* ------------------  FRAME DEFINITION  ----------------- */
/* ADDED SIDE-LABELS PHRASE TO FRAME STATEMENT             */

define frame f-button
   b-update at 1
   b-add    at 10
   b-split  at 19
   b-delete at 28
   b-end    at 37
   with no-box overlay three-d side-labels width 46.

/* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME */
clearFrameRegistration(frame f-button:handle).

/* SET EXTERNAL LABELS */
setFrameLabels(frame f-button:handle).

assign
   l-focus               = b-update:handle in frame f-button
   b-update:width        = 8
   b-update:private-data = "update"
   b-add:width           = 8
   b-add:private-data    = "add"
   b-delete:width        = 8
   b-delete:private-data = "delete"
   b-split:width         = 8
   b-split:private-data  = "split"
   b-end:width           = 8
   b-end:private-data    = "end".

define frame f-1
   skip(.4)
   fad_tag format "x(17)"
   fad_desc format "x(20)"
   fad_puramt format "->>>>>>>>>9.99<"
   l-curr format "x(3)"
   fad_serial format "x(20)"
   with three-d overlay 8 down scroll 1 width 80.

/* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME*/
clearFrameRegistration(frame f-1:handle).

/* SET EXTERNAL LABELS */
setFrameLabels(frame f-1:handle).

run ip-framesetup.

w-frame = frame f-1:handle.
{us/bbi/gprun.i ""fafmtut.p""
   "(w-frame)"}

lines = 8.

/* ---------------  PRE PROCESSING INCLUDE  -------------- */
define new shared workfile temp-select
   field sel_idr    as character format "x(1)"
   field sel_tag    as character format "x(17)"
   field sel_desc   as character format "x(20)"
   field sel_amt    as decimal   format ">>>,>>>,>>9.99<"
   field sel_serial as character format "x(20)".

define new shared temp-table tt_fa_accts no-undo
   fields tt_fa_id     like faba_fa_id
   fields tt_fabk_id   like faba_fabk_id
   fields tt_acct_type like faba_acctype
   fields tt_fa_acct   like faba_acct
   fields tt_fa_sub    like faba_sub
   fields tt_fa_cc     like faba_cc
   index tt_fa_accts is primary tt_fa_id tt_fabk_id tt_acct_type tt_fa_acct.

for first fa_mstr where fa_mstr.fa_domain = global_domain
   and fa_id = l-key
   no-lock: 
end.
if not available fa_mstr
   then
   return.

/* get the primary book currency */
for first fabk_mstr no-lock
   where fabk_domain= global_domain and fabk_primary = yes,
   cu_mstr of fabk_mstr:
   assign 
      l-curr = cu_mstr.cu_curr.
   
	{us/gp/gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
				 "(input l-curr,
					output rndmthd,
					output mc-error-number)"}
	if mc-error-number <> 0 then 
	do:
		{us/bbi/pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=3}
	end.
end.

for first fact_ctrl where fact_ctrl.fact_domain = global_domain
   no-lock: 
end. /* FOR FIRST fact_ctrl */

/* SET CURRENCY DEPENDENT ROUNDING FORMAT */
for first gl_ctrl where gl_ctrl.gl_domain = global_domain
   no-lock: 
end. /* FOR FIRST gl_ctrl */

if rndmthd = ? or rndmthd = '' then
do:
	assign rndmthd = gl_rnd_mthd.
end.

fad_puramt_fmt = fad_det.fad_puramt:format.

{us/bbi/gprun.i ""gpcurfmt.p""
   "(input-output fad_puramt_fmt,
     input rndmthd)"}

fad_det.fad_puramt:format = fad_puramt_fmt.

/* -------------  END PRE PROCESSING INCLUDE  ------------ */

open query q_fad_det
   for each fad_det where fad_det.fad_domain = global_domain
   and  fad_fa_id = l-key
   no-lock.

get first q_fad_det no-lock.

l_amt_fmt = fad_puramt:format in frame f-1.

assign
   fad_puramt:format in frame f-1 = l_amt_fmt.

main-loop:
do while perform-status <> "end"
   on error undo:

   if {us/bbi/gpiswrap.i} then frame f-1:visible = yes.

   run ip-query
      (input-output perform-status,
      input-output l-rowid).

   if {us/bbi/gpiswrap.i} then hide frame f-1 no-pause.

   if perform-status = "delete"
   then do:
      run ip-lock
         (input-output perform-status).
      perform-status = "first".
      run ip-predisplay.
   end. /* IF perform-status = "delete" */

   /* ----------------------  SPLIT  ---------------------- */
   if perform-status = "split" and
      fa_mstr.fa_disp_dt = ?
   then do:

      /* CHECK IF ANY OF THE BOOKS IS SUSPENDED */
      for each faadj_mstr where faadj_mstr.faadj_domain = global_domain
         and faadj_fa_id = fa_mstr.fa_id
         no-lock
         break by faadj_fabk_id:
         if last-of (faadj_fabk_id)
            and faadj_type = "5"
         then do:
            /* SPLIT NOT ALLOWED. ASSET HAS BEEN SUSPENDED */
            {us/bbi/pxmsg.i &MSGNUM=4555 &ERRORLEVEL=4}
            perform-status = "".
            next main-loop.
         end. /* IF LAST-OF (faadj_fabk_id) ... */
      end. /* FOR EACH faadj_mstr */

      for each fad_det where fad_det.fad_domain = global_domain
         and fad_det.fad_fa_id  = l-key
         no-lock:

         assign
            fad-total = fad-total + 1
            fad-cost  = fad-cost  + fad_puramt.

      end. /* FOR EACH fad_det */

      do transaction on error undo:

         for first fa_mstr where fa_mstr.fa_domain = global_domain
            and fa_id = l-key
            exclusive-lock:
            assign
               fa_qty    = fad-total
               fa_puramt = fad-cost
               fad-total = 0
               fad-cost  = 0.

            if recid(fa_mstr) = -1
            then .
         end. /* FOR FIRST fa_mstr */
      end. /* DO TRANSACTION */

      /* PROMPT FOR NEW ASSET, PERCENT, AMOUNT AND COMPONENTS */
      {us/bbi/gprun.i ""fafablc.p""
         "(input  fa_mstr.fa_id,
           output l-new,
           output l-pct,
           output l-amt,
           output l-err-nbr)"}

      /* CREATE NEW ASSET */
      if l-err-nbr = 0 and
         l-new     <> ""
      then do:
         /* CREATING NEW ASSET.  PLEASE WAIT */
         {us/bbi/pxmsg.i &MSGNUM=832 &ERRORLEVEL=1}

         /* ADDED SIXTH, SEVENTH, EIGHTH, NINTH, TENTH, ELEVENTH PARAMETER */
         {us/bbi/gprun.i ""fafabla.p""
            "(input l-key,
              input-output l-new,
              input l-pct,
              input l-amt,
              input fa_mstr.fa_qty,
              input """",
              input """",
              input """",
              input """",
              input no,
              output l-err-nbr)"}

         hide message no-pause.
         if l-err-nbr = 0
         then do:

            /* FOR FIXED ASSET WHICH IS ALREADY POSTED TO GL IS SPLIT */
            /* fa_puramt SHOULD BE ASSIGNED TO fa_posted_puramt */
            for first fa_mstr
               where fa_mstr.fa_domain   = global_domain
               and   fa_mstr.fa_id       = l-new
               and   fa_mstr.fa_acq_post = yes
               exclusive-lock:
               fa_mstr.fa_posted_puramt = fa_mstr.fa_puramt.
            end.

            for first fa_mstr
               where fa_mstr.fa_domain   = global_domain
               and   fa_mstr.fa_id       = l-key
               and   fa_mstr.fa_acq_post = yes
               exclusive-lock:
               fa_mstr.fa_posted_puramt = fa_mstr.fa_puramt.
            end.

            /* NEW ASSET CREATED */
            {us/bbi/pxmsg.i &MSGNUM=3209 &ERRORLEVEL=1 &MSGARG1=l-new}
         end. /* IF l-err-nbr = 0 */
         else 
         do:
            {us/bbi/pxmsg.i &MSGNUM=l-err-nbr &ERRORLEVEL=3}.
         end. /* ELSE DO */
         l-reopen-fa-query = yes.
      end.  /* IF l-err-nbr ... */
      else 
      do:
         /* UNABLE TO CREATE ASSET */
         {us/bbi/pxmsg.i &MSGNUM=3280 &ERRORLEVEL=3}.
      end. /* ELSE DO */

      /* RE-OPEN QUERY TO SHOW REMAINING FAD_DET'S */
      perform-status = "open".
      run ip-query
         (input-output perform-status,
         input-output l-rowid).
      get first q_fad_det no-lock.
      assign
         perform-status = "first"
         l-rowid        = rowid(fad_det).
   end.  /* IF perform-status = "split" ... */
   else
      if perform-status = "split" and
         not fa_mstr.fa_post
      then do:
         /* SPLITING NOT ALLOWED - ASSET HAS NOT BEEN POSTED */
         {us/bbi/pxmsg.i &MSGNUM=3208 &ERRORLEVEL=4}
         perform-status = "".
         next main-loop.
      end. /* IF perform-status = "split" ... */
      else
         if perform-status = "split" and
            fa_disp_dt <> ?
         then do:
            /* SPLITING NOT ALLOWED - ASSET HAS BEEN RETIRED */
            {us/bbi/pxmsg.i &MSGNUM=3210 &ERRORLEVEL=4}
            perform-status = "".
            next main-loop.
         end. /* IF perform-status = "split" ... */
   /* --------------------  END SPLIT  -------------------- */

   /* ----------------------  DISPLAY  ---------------------- */
   if {us/bbi/gpiswrap.i} then frame f-1:visible = yes.

   run ip-predisplay.

   /* -------------------  ADD/UPDATE  -------------------- */
   if (perform-status = "update" or
      perform-status = "add")
   then do:
      run ip-prompt.
      if perform-status = "undo"
      then do:
         perform-status = "first".
         next main-loop.
      end. /* IF perform-status = "undo" */
      run ip-displayedits.
   end. /* IF (perform-status = "update" or ... */

   /* -----------------------  END  ----------------------- */
   if perform-status = "end"
   then do:
      hide frame f-1 no-pause.
      delete PROCEDURE this-procedure no-error.
      leave.
   end. /* IF perform-ststus = "end" */

   /* ----------------  SELECTION BUTTONS  ---------------- */
   if perform-status <> "first"
   then run ip-button
         (input-output perform-status).

   /* -------------  AFTER STRIP MENU INCLUDE  ------------ */
   for first fa_mstr where fa_mstr.fa_domain = global_domain
      and fa_id = l-key
      no-lock: 
   end. /* FOR FIRST fa_mstr */

   if (perform-status = "update" or
      perform-status = "add"    or
      perform-status = "delete" or
      perform-status = "split") and
      fa_disp_dt <> ?
   then do:
      /* NO MODIFICATIONS ALLOWED - ASSET IS RETIRED */
      {us/bbi/pxmsg.i &MSGNUM=3211 &ERRORLEVEL=4}
      perform-status = "".
      next main-loop.
   end. /* IF perform-status ... */
   if perform-status = "end"
   then do:

      assign
         fad-total = 0
         fad-cost  = 0.

      for each fad_det where fad_det.fad_domain = global_domain
         and fad_fa_id = l-key
         no-lock:
         assign
            fad-total = fad-total + 1
            fad-cost  = fad-cost  + fad_puramt.
      end. /* FOR EACH fad_det */

      l-create-dep = fa_puramt <> fad-cost and fa_post = no.

      for each fab_det
         where fab_det.fab_domain = global_domain
         and   fab_det.fab_fa_id  = l-key no-lock,
         each fabk_mstr 
         where fabk_domain = global_domain
         and   fabk_id = fab_fabk_id no-lock
      break by fab_fabk_id:
         if first(fab_fabk_id) or fabk_primary
         then do:
            find cu_mstr
               where cu_mstr.oid_cu_mstr = fabk_mstr.oid_cu_mstr
            no-lock no-error.
            if available cu_mstr
            then l-book-curr = cu_mstr.cu_curr.
         end. /* IF FIRST(fab_fabk_id) OR */
      end. /* FOR EACH fab_det */

      /* GET ROUNDING METHOD FROM CURRENCY MASTER */
      {us/gp/gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
         "(input  l-book-curr,
           output rndmthd,
           output mc-error-number)"}
      if mc-error-number <> 0
      then do:
         {us/bbi/pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}
         leave.
      end. /* IF mc-error-number <> 0 */                   

      {us/gp/gprunp.i "mcpl" "p" "mc-curr-rnd"
         "(input-output fad-cost,
           input rndmthd,
           output mc-error-number)"}

      do transaction on error undo:
         for first fa_mstr where fa_mstr.fa_domain = global_domain
            and fa_id = l-key
            exclusive-lock:
            assign
               fa_qty    = fad-total
               fa_puramt = fad-cost
               fad-total = 0
               fad-cost  = 0.

            if recid(fa_mstr) = -1
            then .
         end. /* FOR FIRST fa_mstr */
      end. /* DO TRANSACTION */

      if l-create-dep = yes
      then do:
         CREATE-DEP:
         do transaction on error undo:
            for each fab_det where fab_det.fab_domain = global_domain
               and fab_fa_id = fa_id
               exclusive-lock:
                               
               find first bfabk_mstr where bfabk_mstr.fabk_domain = global_domain and
                                           bfabk_mstr.fabk_id = fab_det.fab_fabk_id and
                                           bfabk_mstr.fabk_primary = yes
               no-lock no-error.
               if not available bfabk_mstr
               then next.                                                    
                   
               fab_amt = fa_puramt.

               if recid(fab_det) = -1
               then .

               /* ADDED 1ST INPUT PARAMETER AS yes THIS WILL  */
               /* CHECK THAT THE NEXT CALENDAR IS DEFINED FOR */
               /* AN ASSET PLACED IN A SHORT YEAR.            */

               /* ADDED 5TH AND 6TH PARAMETERS AS THE YEAR FOR ADJUSTMENT */
               /* AND DEPRECIATION CALCULATION FOR FOLLOWING YEARS IS     */
               /* NEEDED OR NOT RESPECTIVELY. THESE PARAMETERS WILL BE    */
               /* zero AND no RESPECTIVELY EXCEPT FOR UTILITY utrgendp.p  */
               {us/bbi/gprun.i ""fadpbla.p""
               "(input  yes,
                 input  fab_det.fab_resrv,
                 buffer fa_mstr,
                 buffer fab_det,
                 input  0,
                 input  no,
                 output l-err-nbr)"}

               if l-err-nbr <> 0
               then do:
                  {us/bbi/pxmsg.i &MSGNUM=l-err-nbr &ERRORLEVEL=3}
                  undo CREATE-DEP.
               end. /* IF l-err-nbr */
            end. /* FOR EASH fab_det */
         end.  /* DO TRANSACTION */
      end. /* IF l-create-dep = yes */
   end. /* IF perform-status = "end" */
/* -----------  END AFTER STRIP MENU INCLUDE  ---------- */

end. /* DO WHILE */

/* -------------  ADD / UPDATE / FIELD EDITS  ------------ */
PROCEDURE ip-prompt:
   if perform-status = "add"
   then do:
      scroll from-current down with frame f-1.
      l-rowid = ?.
   end. /* IF perform-status = "add" */

   l-first = yes.
   CASE perform-status:
      when ("add")
      then
         /* ADDING NEW RECORD */
         {us/bbi/pxmsg.i &MSGNUM=1 &ERRORLEVEL=1}
      when ("update")
      then
         /* MODIFYING EXISTING RECORD */
         {us/bbi/pxmsg.i &MSGNUM=10 &ERRORLEVEL=1}
      otherwise
      hide message no-pause.
   END CASE. /* perform-status */
   ststatus = stline[3].
   status input ststatus.

   prompt-for-it:
   repeat:
      l-first = no.
      repeat:
         assign
            fad_puramt:format in frame f-1 = l_amt_fmt.

         prompt-for
            fad_det.fad_tag
            when (perform-status = "add")
            fad_det.fad_desc
            fad_det.fad_puramt
            fad_det.fad_serial
            with frame f-1.

         /* FIELD EDIT FOR fad_tag */
         l-error = no.
         if perform-status = "add" and
            input frame f-1 fad_tag = ""
         then do:
            /* BLANK NOT ALLOWED */
            {us/bbi/pxmsg.i &MSGNUM=40 &ERRORLEVEL=3}
            l-error = yes.
         end. /* IF perform-status = "add" */
         else
            if perform-status = "add" and
               can-find(first fad_det where fad_det.fad_domain = global_domain
               and fad_tag = input frame f-1 fad_tag)
            then do:
               /* DUPLICATE RECORD - CANNOT CREATE */
               {us/bbi/pxmsg.i &MSGNUM=2041 &ERRORLEVEL=3}
               l-error = yes.
            end. /* IF perform-status = "add" */
         if l-error
         then do:
            next-prompt fad_tag with frame f-1.
            next.
         end. /* IF l-error */
         /* END FIELD EDIT FOR fad_tag */

         /* FIELD EDIT FOR fad_puramt */
         l-error = no.
         /* CHECK CURRENCY DEPENDENT ROUNDING FORMAT */
         {us/bbi/gprun.i ""gpcurval.p""
            "(input input frame f-1 fad_det.fad_puramt,
              input rndmthd,
              output l-err-nbr)"}

         if l-err-nbr <> 0
         then
            l-error = yes.

         if l-error
         then do:
            next-prompt fad_puramt with frame f-1.
            next.
         end. /* IF l-error */
         /* END FIELD EDIT FOR fad_puramt */

         leave.
      end.

      if keyfunction(lastkey) = "end-error"
      then do:
         perform-status = "undo".
         return no-apply.
      end. /* if keyfunction(lastkey) = "end-error" */
      leave.
   end. /* prompt-for-it */

   if keyfunction(lastkey) = "end-error"
   then do:
      if perform-status = "add"
      then do:
         /* RECORD NOT ADDED */
         {us/bbi/pxmsg.i &MSGNUM=1304 &ERRORLEVEL=1}
         get first q_fad_det no-lock.
         if available fad_det
         then do:
            assign
               perform-status = "first"
               l-rowid        = rowid(fad_det).
         end. /* IF AVAILABLE fad_det */
         else
            assign
               perform-status = ""
               l-rowid        = ?.
         clear frame f-1 no-pause.
      end. /* IF perform-status = "add" */
      else
         if available fad_det
         then
            assign
               perform-status = ""
               l-rowid        = rowid(fad_det).
      return.
   end. /* IF key(function(lastkey) */
   run ip-lock
      (input-output perform-status).
END PROCEDURE. /* ip-promt */

/* -----------------------  LOCKING  ----------------------- */
PROCEDURE ip-lock:
   define input-output parameter perform-status as character no-undo.

   if perform-status = "add" or
      perform-status = "update" or
      perform-status = "delete"
   then

   ip-lock:
   do transaction on error undo:
      assign
         pos         = 0
         l-delete-it = yes.

      if available fad_det
      then
         get current q_fad_det no-lock.

      if available fad_det and
         current-changed fad_det
      then do:
         l-delete-it = yes.
         /* RECORD HAS BEEN MODIFIED SINCED YOU EDITED IT SAVE ANYWAY */
         {us/bbi/pxmsg.i &MSGNUM=2042 &ERRORLEVEL=1 &CONFIRM=l-delete-it}

         if l-delete-it = no
         then do:
            hide message no-pause.
            run ip-displayedits.
            return.
         end. /* IF l-delet-it */
         hide message no-pause.
      end. /* IF AVAILABLE fad_det */
      if available fad_det
      then
      tran-lock:
      do while perform-status <> "":
         get current q_fad_det
            exclusive-lock no-wait.
         if locked fad_det
         then do:
            if pos < 3
            then do:
               pos = pos + 1.
               pause 1 no-message.
               next tran-lock.
            end. /* IF pos < 3 */
            perform-status = "error".
         end. /* IF LOCKED fad_det */
         leave.
      end. /* WHILE */

      if (perform-status = "update" or
         p-status       = "update") and
         l-delete-it    = yes
      then
         run ip-update
            (input-output perform-status).
      if perform-status = "delete"
      then
         run ip-delete
            (input-output perform-status).

      if available fad_det
      then
         get current q_fad_det no-lock.
   end. /* DO TRANSACTION */

   if perform-status = "add"
   then
      run ip-add
         (input-output perform-status).
END PROCEDURE. /* ip-lock */

PROCEDURE ip-update:
   define input-output parameter perform-status as character no-undo.

   if p-status = "update"
   then
      p-status = "".

   run ip-assign
      (input-output perform-status).
   perform-status = "open".
   run ip-query
      (input-output perform-status,
      input-output l-rowid).
   reposition q_fad_det to rowid l-rowid no-error.
   get next q_fad_det
      no-lock.
   assign
      perform-status = "first"
      l-rowid        = rowid(fad_det).
   return.
END PROCEDURE. /* ip-update */

PROCEDURE ip-add:
   define input-output parameter perform-status as character no-undo.

   create fad_det.
   fad_det.fad_domain = global_domain.
   run ip-assign
      (input-output perform-status).
   perform-status = "open".
   run ip-query
      (input-output perform-status,
      input-output l-rowid).
   reposition q_fad_det to rowid l-rowid no-error.
   get next q_fad_det no-lock.
   assign
      perform-status = "first"
      l-rowid        = rowid(fad_det).
   return.
END PROCEDURE. /* ip-add */

PROCEDURE ip-delete:
   define input-output parameter perform-status as character no-undo.

   l-delete-it = no.

   /* LOCKED-DELETE-EDIT */
   /* DO NOT ALLOW DELETE IF ASSET DOES */
   /* NOT HAVE AT LEAST ONE COMPONENT   */
   define buffer faddet for fad_det.
   fad-total = 0.

   for each faddet where faddet.fad_domain = global_domain
      and faddet.fad_fa_id = l-key
      no-lock:
      fad-total = fad-total + 1.
   end. /* FOR EACH faddet */

   if fad-total = 1
   then do:
      /* ASSET MUST HAVE AT LEAST ONE COMPONENT */
      {us/bbi/pxmsg.i &MSGNUM=3212 &ERRORLEVEL=4}
      perform-status = "".
      return.
   end. /* IF fad-total */
   fad-total = 0.
   /* LOCKED-DELETE-EDIT */

   /* PLEASE CONFIRM DELETE */
   {us/bbi/pxmsg.i &MSGNUM=11 &ERRORLEVEL=1 &CONFIRM=l-delete-it}
   CASE l-delete-it:
      when yes
      then 
         do:
            hide message no-pause.
            delete fad_det.
            clear frame f-1 no-pause.
            get next q_fad_det no-lock.
            if available fad_det
            then do:
               assign
                  perform-status = "first"
                  l-rowid        = rowid(fad_det).
            end. /* IF AVAILABLE fad_det */
            else 
            do:
               get prev q_fad_det no-lock.
               if available fad_det
               then do:
                  assign
                     perform-status = "first"
                     l-rowid        = rowid(fad_det).
               end. /* IF AVAILABLE fad_det */
               else
                  assign
                     perform-status = "first"
                     l-rowid        = rowid(fad_det).
            end. /* ELSE DO */
            /* RECORD DELETED */
            {us/bbi/pxmsg.i &MSGNUM=22 &ERRORLEVEL=1}
            return.
         end. /* l-delete-it */
      otherwise
      run ip-displayedits.
   END CASE.
END PROCEDURE. /* ip-delete */

PROCEDURE ip-assign:
   define input-output parameter perform-status as character no-undo.
   do with frame f-1:
      assign
         fad_det.fad_desc
         fad_det.fad_puramt
         fad_det.fad_mod_userid = global_userid
         fad_det.fad_mod_date   = today
         fad_det.fad_fa_id      = l-key
         fad_det.fad_serial
         fad_det.fad_tag
         l-rowid                = rowid(fad_det).

      /* --------  /* AFTER-ASSIGN-AUDIT-INCLUDE */  ----------- */
      for first fab_det where fab_det.fab_domain = global_domain
         and fab_fa_id = l-key
         and can-find(first fabk_mstr where fabk_domain = global_domain
         and fabk_id     = fab_fabk_id
         and fabk_primary   = yes)
         no-lock:
      end. /* FOR FIRST fab_det */
      if available fab_det
      then do:
         fad-total = 0.
         for each fad_det where fad_det.fad_domain = global_domain
            and fad_fa_id = l-key
            no-lock:
            fad-total       = fad-total + fad_puramt.
         end. /* FOR EACH fad_det */

         if fad-total <> fab_amt
         then do:
            /* COMPONENT AMOUNTS DO NOT TOTAL TO POSTING BOOK BASIS AMOUNT */
            {us/bbi/pxmsg.i &MSGNUM=3207 &ERRORLEVEL=2}
         end. /* IF fad-total <> */
      end.  /* IF AVAILABLE fab_det */
      for first fad_det
         where rowid(fad_det) = l-rowid
         no-lock:
      end. /* FOR FIRST fad_det */

   /* ------  END /* AFTER-ASSIGN-AUDIT-INCLUDE */  ----------- */

   end. /* DO WITH FRAME */
END PROCEDURE. /* ip-assign */

PROCEDURE ip-predisplay:
   if (perform-status = ""        or
      perform-status  = "update") and
      available fad_det
   then
   display-loop:
   do:
      if perform-status = ""
      then do:
         clear frame f-1 all no-pause.
         l-top-rowid = rowid(fad_det).
      end. /* IF perform-status = "" */
      read-loop:
      do pos = 1 to lines:
         if perform-status = ""
         then do:
            if session:display-type = "gui"
            then
               assign
                  fad_tag:bgcolor = 8
                  fad_tag:fgcolor = 0.
            else
               color display normal fad_tag
                  with frame f-1.
         end. /* IF perform-status = "" */
         run ip-displayedits.
         if perform-status = "update"
         then
            leave display-loop.

         if pos < lines
         then
            down with frame f-1.

         get next q_fad_det no-lock.

         if not available fad_det
         then
            leave.
      end. /* IF perform-status */
      run ip-postdisplay.
   end. /* DO */
END PROCEDURE. /* ip-predisplay */

PROCEDURE ip-displayedits:
   if available fad_det
   then do:

      assign
         fad_det.fad_puramt:format in frame f-1 = l_amt_fmt.

      run ip-display.
   end. /* IF AVAILABLE fad_det */
END PROCEDURE. /* ip-predisplayedits */

PROCEDURE ip-display:
   display
      fad_det.fad_desc
      fad_det.fad_puramt
      fad_det.fad_serial
      fad_det.fad_tag
      l-curr
      with frame f-1.
END PROCEDURE. /* ip-display */

PROCEDURE ip-postdisplay:
   do pos = 1 to lines:
      if frame-line(f-1) <= 1
      then
         leave.
      up 1 with frame f-1.
   end. /* DO pos = 1 */
   if perform-status = ""
   then do:
      reposition q_fad_det to rowid l-top-rowid no-error.
      get next q_fad_det no-lock.
      l-rowid = l-top-rowid.
      color display message fad_tag
         with frame f-1.
   end. /* IF perform-status = "" */
END PROCEDURE. /* ip-postdislay */

PROCEDURE ip-query:
   define input-output parameter perform-status as character no-undo.
   define input-output parameter l-rowid        as rowid     no-undo.

   if perform-status = "first"
   then do:
      if l-rowid <> ?
      then do:
         reposition q_fad_det to rowid l-rowid no-error.
         get next q_fad_det no-lock.
      end. /* IF l-row <> ? */
      if not available fad_det
      then
         get first q_fad_det no-lock.
      if available fad_det
      then
         assign
            perform-status = ""
            l-rowid        = rowid(fad_det).
      else 
      do:
         assign
            perform-status    = ""
            l-rowid           = ?
            frame f-1:visible = true.
         return.
      end. /* ELSE DO */
   end. /* IF perform-status = "first" */

   if perform-status = "open"
   then do:
      open query q_fad_det
         for each fad_det where fad_det.fad_domain = global_domain
         and fad_fa_id = l-key
         use-index fad_fa_id no-lock.

      reposition q_fad_det to rowid l-rowid no-error.
      get next q_fad_det no-lock.
      if available fad_det
      then
         assign
            perform-status = ""
            l-rowid        = rowid(fad_det).
      else 
      do:
         get first q_fad_det no-lock.
         if not available fad_det
         then do:
            assign
               perform-status    = ""
               frame f-1:visible = true.
            return.
         end. /* IF NOT AVAILABLE fad_det */
         else
            assign
               perform-status = ""
               l-rowid        = rowid(fad_det).
      end. /* FOR EACH fad_det */
   end. /* IF perform-status = "open" */

   if perform-status = "next"
   then do:
      get next q_fad_det no-lock.
      if available fad_det
      then do:
         hide message no-pause.
         assign
            l-rowid        = rowid(fad_det)
            perform-status = "next".

         if session:display-type = "gui"
         then
            assign
               fad_tag:bgcolor = 8
               fad_tag:fgcolor = 0.
         else
            color display normal fad_tag
               with frame f-1.
         pause 0.  /* MAKES SCROLLING WORK - DON'T REMOVE */
         down 1 with frame f-1.
         run ip-displayedits.
         color display message fad_tag
            with frame f-1.
      end. /* IF AVAILABLE fad_det */
      else 
      do:
         perform-status = "next".
         /* END OF FILE */
         {us/bbi/pxmsg.i &MSGNUM=20 &ERRORLEVEL=2}
         get last q_fad_det no-lock.
      end. /* ELSE DO */
   end. /* IF perform-status = "next" */

   if perform-status = "prev"
   then do:
      get prev q_fad_det no-lock.
      if not available fad_det
      then do:
         /* BEGINNING OF FILE */
         {us/bbi/pxmsg.i &MSGNUM=21 &ERRORLEVEL=2}
         get first q_fad_det no-lock.
         return.
      end. /* IF NOT AVAILABLE fad_det */
      hide message no-pause.
      l-rowid = rowid(fad_det).
      if session:display-type = "gui"
      then
         assign
            fad_tag:bgcolor = 8
            fad_tag:fgcolor = 0.
      else
         color display normal fad_tag
            with frame f-1.
      pause 0.  /* MAKES SCROLLING WORK - DON'T REMOVE */
      up 1 with frame f-1.
      run ip-displayedits.
      color display message fad_tag
         with frame f-1.
   end. /* IF perform-status = "prev" */

   if perform-status = "update" or
      perform-status = "delete"
   then do:
      get current q_fad_det no-lock.
      if not available fad_det
      then do:
         hide message no-pause.
         /* RECORD NOT FOUND */
         {us/bbi/pxmsg.i &MSGNUM=1310 &ERRORLEVEL=3}
         perform-status = "".
      end. /* IF NOT AVAILABLE fad_det */
   end. /* IF perform-status = "update" ... */
END PROCEDURE. /* ip-query */

PROCEDURE ip-framesetup:
   if session:display-type = "gui"
   then
      frame f-1:row = 4.5.
   else
      frame f-1:row = 4.

   assign
      frame f-1:box           = true
      frame f-1:centered      = true
      frame f-1:title         = (getFrameTitle("ASSET_COMPONENT_MAINTENANCE",38))
      frame f-button:centered = true
      frame f-button:row      = 20.

   if {us/bbi/gpiswrap.i} then frame f-1:overlay = false.

END PROCEDURE. /* ip-framesetup */

PROCEDURE ip-button:
   define input-output parameter perform-status as character
      format "x(25)" no-undo.

   if not batchrun
   then do:
      enable all with frame f-button.
      ststatus = stline[2].
      status input ststatus.
      on choose of b-update
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle.
            hide frame f-button no-pause.
         end. /* ON CHOOSE OF b-update */
      on choose of b-add
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle.
            hide frame f-button no-pause.
         end. /* ON CHOOSE OF b-add */
      on choose of b-delete
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle.
            hide frame f-button no-pause.
         end. /* ON CHOOSE OF b-delete */
      on choose of b-split
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle
               l-title        = current-window:title
                             + " - "
                             + self:label.
            hide frame f-button no-pause.
         end. /* ON CHOOSE OF b-split */
      on choose of b-end
         do:
            assign
               perform-status = self:private-data
               l-focus        = self:handle.
            hide frame f-1 no-pause.
            hide frame f-button no-pause.
         end. /* ON CHOOSE OF b-end */
      on cursor-up, f9 anywhere
         do:
            assign
               perform-status = "prev"
               l-focus        = focus:handle.
            apply "go" to frame f-button.
         end. /* ON CURSOR-UP, f9 anywhere */
      on cursor-down, f10 anywhere
         do:
            assign
               perform-status = "next"
               l-focus        = focus:handle.
            apply "go" to frame f-button.
         end. /* ON CURSOR-DOWN, f10 anywhere */
      on cursor-right anywhere
         do:
            l-focus = self:handle.
            apply "tab" to l-focus.
         end. /* ON CURSOR-RIGHT anywhere */
      on cursor-left anywhere
         do:
            l-focus = self:handle.
            if session:display-type = "gui"
            then
               apply "shift-tab" to l-focus.
            else
               apply "ctrl-u" to l-focus.
         end. /* ON CURSOR-LEFT anywhere */
      on f3 anywhere
         apply "choose" to b-add    in frame f-button.
      on f5, ctrl-d anywhere
         apply "choose" to b-delete in frame f-button.
      on esc anywhere
         do:
            if session:display-type = "gui"
            then
               apply "choose" to b-end in frame f-button.
         end. /* ON ESC anywhere */
      on end-error anywhere
         apply "choose" to b-end in frame f-button.
      on window-close of current-window
         apply "choose" to b-end in frame f-button.
      on go anywhere
         do:
            if (lastkey = keycode("cursor-down") or
               lastkey = keycode("cursor-up") or
               lastkey = keycode("f9") or
               lastkey = keycode("f10"))
            then
               return.
            else
               l-focus = focus:handle.
            apply "choose" to l-focus.
         end. /* ON GO anywhere */
      wait-for
         go of frame f-button or
         window-close of current-window or
         choose of
         b-update,
         b-add,
         b-delete,
         b-split,
         b-end
         focus
         l-focus.

   end. /* IF NOT BATCHRUN */
   else
      set perform-status.

   hide message no-pause.
END PROCEDURE. /* ip-button */
